#include "QuadProcess.h"
#include "GraphicsManager.h"
#include "Font/FontManager.h"
#include "Math/Matrix44.h"
#include "Math\Vector2.h"
#include "Base.h"
#include "InputManager.h"
#include "Camera\Object3D.h"
#include "Camera\ThPSCamera\ThPSCamera.h"
#include "RenderableVertexs\RenderableVertexs.h"
#include "Textures\Texture.h"
#include "Textures\2DTexture.h"
#include "StaticMeshes\StaticMesh.h"

TTEXTURED_VERTEX l_Vtxs2[4]={
	{12,8,0,0,0},
	{16,8,0,1,0},
	{12,3,0,0,1},
	{16,3,0,1,1}};
TCOLORED_VERTEX l_Vtx[4] = {
    {2,8,0, 0xFFFFFFFF},
    {6,8,0, 0xFFFFFFFF},
    {2,3,0, 0xFFFFFFFF},
    {6,3,0, 0xFFFFFFFF}};
unsigned short l_Idxs[6] = {0,1,2,2,1,3};
CRenderableVertexs *g_RV2;
CRenderableVertexs *g_RV;
CTexture *g_Texture;

CQuadProcess::CQuadProcess(void) : m_x(0), m_y(0), m_Dir1(DOWN), m_speed(5), m_time(0), m_GameOver(false), m_Colisionar(0), m_Menjar(0),
m_Puntuacio(0), m_Object(0), m_Yaw(0), m_Pitch(0), m_Pos(Vect3f(0, 0, 0)), m_Dir(Vect3f(0, 0, 0)), m_Canvi(false),
CProcess(), m_Direccio(false), m_IsVisible(false), m_Camera(0), m_ThPSCamera(0), m_FPSCamera(0), m_Static(0){}
CQuadProcess::~CQuadProcess(void)
{
	DeInit();
}

void CQuadProcess::Init()
{
	m_Object = new CObject3D(Vect3f(2,0.5,2), 0 ,0);
	m_Object1 = new CObject3D(Vect3f(0,1,0), 0 ,0);
	m_ThPSCamera = new CThPSCamera(1.0f, 100.0f, 45.0f * D3DX_PI / 180.0f ,1.0f,m_Object,5.0f);
	SetCamera(m_ThPSCamera);
	m_FPSCamera = new CFPSCamera(1.0f, 100.0f, 45.0f * D3DX_PI / 180.0f ,1.0f,m_Object1);
	g_Texture = new C2DTexture();
	//g_Texture->Load("./Data/tiger.bmp");
	m_Static = new CStaticMesh();
	m_Static->Load("./Data/static_meshes/Box001.mesh");
	g_RV2 = new CIndexedVertexs<TTEXTURED_VERTEX>(CORE->GetGraphicsManager(), l_Vtxs2, l_Idxs, 4, 6);
	g_RV = new CIndexedVertexs<TCOLORED_VERTEX>(CORE->GetGraphicsManager(), l_Vtx, l_Idxs, 4, 6);
}

void CQuadProcess::DeInit()
{
	CHECKED_DELETE(m_ThPSCamera);
	CHECKED_DELETE(m_FPSCamera);
	CHECKED_DELETE(m_Camera);
	CHECKED_DELETE(m_Object);
	CHECKED_DELETE(m_Object1);
	CHECKED_DELETE(g_Texture);
	CHECKED_DELETE(g_RV2);
	CHECKED_DELETE(g_RV);
	CHECKED_DELETE(m_Static);
}

void CQuadProcess::Update(float l_DeltaTime)
{	
	if(CORE->GetInputManager()->IsUpDown(IDV_KEYBOARD,KEY_C))
	{
		if(m_Camera->GetTypeCamera()==0)
		{
			m_ThPSCamera->SetObject3D(m_Object);
			SetCamera(m_ThPSCamera);
		} else if(m_Camera->GetTypeCamera()==1)
		{
			m_FPSCamera->SetObject3D(m_Object1);
			SetCamera(m_FPSCamera);
		}
	}
	if(CORE->GetInputManager()->IsUpDown(IDV_KEYBOARD,KEY_O))
	{
		if(m_Camera->GetObject3D()==m_Object)
		{
			m_FPSCamera->SetObject3D(m_Object1);
			SetCamera(m_FPSCamera);
			//para ejemplo de dos cameras con dos objetos i el drawcube, treure 
			//m_ThPSCamera->SetObject3D(m_Object1);
		} else if(m_Camera->GetObject3D()==m_Object1)
		{
			//para ejemplo de dos cameras con dos objetos i el drawcube
			//m_FPSCamera->SetObject3D(m_Object);
			m_ThPSCamera->SetObject3D(m_Object);
			SetCamera(m_ThPSCamera);
		}
	}
	float l_Yaw=m_Object1->GetYaw();
	if(l_Yaw>=ePI2f)
	{
		m_Direccio=true;
	}
	if(l_Yaw<=0.0)
	{
		m_Direccio=false;
	}
	if(m_Direccio)
		m_Object1->SetYaw(l_Yaw-1*l_DeltaTime);
	else
		m_Object1->SetYaw(l_Yaw+1*l_DeltaTime);
	m_Yaw-=(float)CORE->GetInputManager()->GetMouseDelta().x*0.001f;
	m_Pitch +=(float)CORE->GetInputManager()->GetMouseDelta().y*0.001f;
	m_Object->SetYaw(m_Yaw);
	m_Object->SetPitch(m_Pitch);
	m_Dir=Vect3f(cos(m_Yaw), 0, sin(m_Yaw));
	m_Pos =m_Object->GetPosition();
	if(CORE->GetInputManager()->IsDown(IDV_KEYBOARD,KEY_UP))
	{
		m_Pos+=m_Dir*l_DeltaTime*10;
	} else if(CORE->GetInputManager()->IsDown(IDV_KEYBOARD,KEY_DOWN))
	{
		m_Pos-=m_Dir*l_DeltaTime*10;
	} 
	if(CORE->GetInputManager()->IsDown(IDV_KEYBOARD,KEY_LEFT))
	{
		m_Pos+=m_Dir^Vect3f(0,1,0)*l_DeltaTime*10;
	} else if(CORE->GetInputManager()->IsDown(IDV_KEYBOARD,KEY_RIGHT))
	{
		m_Pos-=m_Dir^Vect3f(0,1,0)*l_DeltaTime*10;
	}
	m_Object->SetPosition(m_Pos);
	m_IsVisible=CORE->GetGraphicsManager()->SphereVisible(Vect3f(0,0,0),0.5);
	

	
}

void CQuadProcess::RenderScene(CGraphicsManager *l_GraphicsManager,float l_DeltaTime)
{
	Mat44f l_Rotate, l_Translate, l_Matrix;
	Mat44f l_Rotate1, l_Translate1, l_Matrix1;
	
	l_Matrix.SetIdentity();
	l_GraphicsManager->SetTransform(l_Matrix);
	l_GraphicsManager->Debug_DrawAxis(5);
	l_GraphicsManager->Debug_DrawGrid(10,10,20,colCYAN);
	//l_Matrix.SetIdentity();
	//l_Translate.SetIdentity();
	//l_Rotate.SetIdentity();
	//l_Translate.Translate(m_Object->GetPosition());
	//l_Rotate.RotByAngleY(-m_Yaw);
	//l_Matrix=l_Translate*l_Rotate;
	//l_GraphicsManager->SetTransform(l_Matrix);
	////l_GraphicsManager->Debug_DrawCube(1,colRED);
	//
	//l_Matrix1.SetIdentity();
	//l_GraphicsManager->SetTransform(l_Matrix1);
	//l_Matrix1.SetIdentity();
	//l_Translate1.SetIdentity();
	//l_Rotate1.SetIdentity();
	//l_Translate1.Translate(m_Object1->GetPosition());
	//l_Rotate1.RotByAngleY(-m_Object1->GetYaw());
	//l_Matrix1=l_Translate1*l_Rotate1;
	//l_GraphicsManager->SetTransform(l_Matrix1);
	//l_GraphicsManager->Debug_DrawCube(1,colYELLOW);
	/*l_Matrix1.SetIdentity();
	l_GraphicsManager->SetTransform(l_Matrix1);
	l_GraphicsManager->DrawCamera(m_FPSCamera);
	l_Matrix1.SetIdentity();
	l_GraphicsManager->SetTransform(l_Matrix1);
	l_GraphicsManager->DrawCamera(m_ThPSCamera);*/



	l_Matrix1.SetIdentity();
	l_GraphicsManager->SetTransform(l_Matrix1);
	m_Static->Render(l_GraphicsManager);
	/*g_Texture->Activate(0);
	g_RV2->Render(l_GraphicsManager);
	g_RV->Render(l_GraphicsManager);*/
}

void CQuadProcess::RenderScene2D(CGraphicsManager *l_GraphicsManager, CFontManager *l_FontManager,float l_DeltaTime)
{
	//CColor l_Color=colBLUE;
	//CColor l_Color1=colBLACK;
	//l_Color.SetAlpha(0.2);
	////l_GraphicsManager->DrawQuad2D(Vect2i(400,300),200,200,CENTER,l_Color);
	//l_GraphicsManager->DrawRectangle2D(Vect2i(400,300),200,200,l_Color,5,5,l_Color1);
	//RenderDebugInfo(l_GraphicsManager, l_FontManager, 1/l_DeltaTime);
	if(m_IsVisible)
	{
		l_FontManager->DrawDefaultText(10,10, colBLACK, "Sphere vista");
	}
}


uint32 CQuadProcess::RenderDebugInfo(CGraphicsManager *l_GraphicsManager, CFontManager *l_FontManager, float l_Fps)
{
	//CProcess::RenderDebugInfo(l_GraphicsManager, l_FontManager, l_Fps);
	return 0;
}
